package com.yowasa.xiuxian.engine.message.hone;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.HoneEngine;
import com.yowasa.xiuxian.engine.hone.MentalInterface;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import com.yowasa.xiuxian.util.DateXUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

@CMD(name = "/开始修炼", scope = CmdScopeEnum.ALL)
public class StartGetExpHandler extends CmdMessageHandler {
    @Autowired
    private UserHelper userHelper;
    @Autowired
    private MessageHelper messageHelper;

    @Override
    public void execute(SendMessage message) {
        UserBO user = userHelper.checkOpUserAll(message);
        if (StringUtils.isBlank(user.getUserConfig().getMental())) {
            throw new HandlerException("你没有学习过修仙之法，无从修炼（需要先学习心法）");
        }
        if (user.getLevel() > 100) {
            throw new HandlerException("你已修炼圆满，进无可进，无法再修炼了");
        }

        user.setLockEndTime(DateXUtil.getMaxTime());
        user.getUserConfig().setGetExpStartTime(new Date());
        user.getUserConfig().setXiuLianFlag(true);
        user.setLockReason(user.getName()+"正在修炼中,请先结束修炼");
        MentalInterface mental = HoneEngine.mentalMap.get(user.getUserConfig().getMental());
        int expect = mental.getExpect(user);
        userHelper.saveUserInfo(user);
        messageHelper.replyMys(message, "开始修炼,你感觉" + getDesc(expect));
    }

    private String getDesc(int expect) {
        if (expect < 10) {
            return "不出十日便可突破";
        } else if (expect < 30) {
            return "最多一月便可突破";
        } else if (expect < 180) {
            return "研习数月可以突破";
        } else if (expect < 360) {
            return "钻研一年可以突破";
        } else if (expect < 360 * 5) {
            return "苦修数年方可突破";
        } else if (expect < 360 * 10) {
            return "不出十年便可突破";
        } else {
            int i = expect / 360;
            return "需要大约"+i+"年才可突破";
        }
    }

    public String desc(){
        return new StringBuffer().append("开始修炼，会根据当前境界计算修炼到圆满所需的大概时间\n")
                .append("苦修数年即 1-5年，不出十年即 5-10年\n")
                .append("苦修百年即 10-100年，即高于10年都会显示苦修百年，而并非真的需要百年才可突破")
                .toString();

    }
}
